import './index.css';
import App from './App';
import './public-path.js';
import reportWebVitals from './reportWebVitals';
import { BrowserRouter } from 'react-router-dom'
import ReactDOM from 'react-dom/client';

let root: ReactDOM.Root | null = null;

function createRoot(props: Record<string, any>) {
  const { container } = props;
  root = ReactDOM.createRoot(container ? container.querySelector('#root') : document.querySelector('#root'));
}

if (!window.__POWERED_BY_QIANKUN__) {
  createRoot({});
  (root as any)?.render(
    <BrowserRouter>
      <App />
    </BrowserRouter>
  );
}

export async function bootstrap() {
  console.log('[react16] react app bootstraped');
}

export async function mount(props: Record<string, any>) {
  console.log('[react16] react app mount');
  createRoot(props);
  root?.render(
    <BrowserRouter basename={window.__POWERED_BY_QIANKUN__ ? '/app-react' : '/'}>
      <App />
    </BrowserRouter>
  )
}

export async function unmount() {
  root?.unmount();
}

reportWebVitals();
